package com.cyz.common.utils;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 进行属性数据转移的工具类
 */
public class BeanCopyUtil {

    public static <T> T copyBean(Object data,Class<T> clazz){
        T result = null;
        try {
            result = clazz.newInstance();
            BeanUtils.copyProperties(data,result);
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return result;
    }

    public static <D,T> List<T> copyBeanList(List<D> list,Class<T> clazz){
        return list.stream()
                .map(item -> copyBean(item,clazz))
                .collect(Collectors.toList());
    }

}
